package com.alks.function.service.impl.chengKong;

import com.alks.common.aop.AutoPageAop;
import com.alks.common.data.dto.SysUserTokenDTO;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.service.RedisService;
import com.alks.entity.data.entity.YfColorDetail;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.dto.chengKong.assort.ColorSystemDto;
import com.alks.function.data.dto.chengKong.assort.YfColorDetail2Dto;
import com.alks.function.data.dto.chengKong.assort.YfColorDetailDto;
import com.alks.function.data.request.chengKong.ColorSystemRequest;
import com.alks.function.data.request.chengKong.MonochromeRequest;
import com.alks.function.data.request.chengKong.MonochromeRequest2;
import com.alks.function.data.request.chengKong.PolychromeRequest;
import com.alks.function.mapper.chengKong.ColorAttributeMapper;
import com.alks.function.service.chengKong.ColorAttributeService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
public class ColorAttributeServiceImpl implements ColorAttributeService {

    @Autowired
    private ColorAttributeMapper colorAttributeMapper;

    @Autowired
    private RedisService redisService;

    /**
     * 单色系明细
     * @param request
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo getMonochromeList(MonochromeRequest request) {

        HashMap map = new HashMap<>();

        //复合查询
        List<YfColorDetailDto> monochromeList = colorAttributeMapper.getMonochromeList(request);
        PageInfo list = new PageInfo(monochromeList);

        map.put("monochromeList", monochromeList);
        map.put("total", list.getTotal());

        return ResponseInfo.ok(map);
    }

    /**
     * 单色系添加
     * @param yfColorDetail
     * @return
     */
    @Override
    public ResponseInfo getMonochromeAdd(YfColorDetail yfColorDetail) {

        if (StringUtils.isEmpty(yfColorDetail.getColor())) {
            return ResponseInfo.error("请输入颜色!");
        }

        //查询国际标准色卡
        String pantone = colorAttributeMapper.getPantone(yfColorDetail.getPantone());
        if (!StringUtils.isEmpty(pantone)) {
            return ResponseInfo.error("PANTONE已经存在!");
        }

        //查询颜色
        YfColorDetailDto color = colorAttributeMapper.getColor(yfColorDetail.getPantone()+yfColorDetail.getColor());
        if (color != null) {
            return ResponseInfo.error("颜色已经存在!");
        }

        //获取用户名
        SysUserTokenDTO dto = UserIdThread.get();
        String userName = dto.getUserName();

        yfColorDetail.setSysUser(userName);
        yfColorDetail.setSysDate(new Date());
        yfColorDetail.setColorSystem("单色系");
        yfColorDetail.setColorName(yfColorDetail.getPantone() + yfColorDetail.getColor());

        //查询颜色编号
        String colorCode = colorAttributeMapper.getMonochromeColorCode("A");
        yfColorDetail.setColorCode(colorCode);
        colorAttributeMapper.addMonochrome(yfColorDetail);

        return ResponseInfo.ok("添加成功!");
    }

    /**
     * 单色系修改
     * @param yfColorDetail
     * @return
     */
    @Override
    public ResponseInfo getMonochromeUpdate(YfColorDetail yfColorDetail) {

        //获取用户名
        SysUserTokenDTO dto = UserIdThread.get();
        String userName = dto.getUserName();

        yfColorDetail.setSysUser(userName);
        yfColorDetail.setSysDate(new Date());

        colorAttributeMapper.updateMonochrome(yfColorDetail);
        return ResponseInfo.ok("修改成功!");
    }

    /**
     * 单色系删除
     *
     * @param request@return
     */
    @Override
    public ResponseInfo getMonochromeDel(MonochromeRequest2 request) {
        if (StringUtils.isEmpty(request.getColorName()) || StringUtils.isEmpty(request.getColorSystem())) {
            return ResponseInfo.error("颜色/色系不能为空!");
        }
        Integer count = colorAttributeMapper.getYfBomMtl(request.getColorName());
        if (count != null && count > 0) {
            return ResponseInfo.error("该颜色已使用,不能删除!");
        }
        colorAttributeMapper.delMonochrome(request);
        return ResponseInfo.ok("删除成功!");
    }

    /**
     * 插色系明细
     * @param request
     * @return
     */
    @Override
    @AutoPageAop
    public ResponseInfo getPolychromeList(PolychromeRequest request) {

        HashMap map =new HashMap();

        List<YfColorDetail2Dto> polychromeList = colorAttributeMapper.getPolychromeList(request);

        PageInfo list=new PageInfo(polychromeList);

        map.put("polychromeList",polychromeList);
        map.put("total",list.getTotal());
        return ResponseInfo.ok(map);
    }

    /**
     * 插色系添加
     * @param yfColorDetail
     * @return
     */
    @Override
    public ResponseInfo getPolychromeAdd(YfColorDetail yfColorDetail) {

        if (StringUtils.isEmpty(yfColorDetail.getColorName())){
            return ResponseInfo.error("请输入材料颜色!");
        }

        String colorName = colorAttributeMapper.getColorName(yfColorDetail.getColorName());
        if (!StringUtils.isEmpty(colorName)){
            return ResponseInfo.error("材料颜色已经存在!");
        }

        //获取用户名
        SysUserTokenDTO dto = UserIdThread.get();
        String userName = dto.getUserName();

        yfColorDetail.setSysUser(userName);
        yfColorDetail.setSysDate(new Date());
        yfColorDetail.setColorSystem("插色系");

        String colorCode = colorAttributeMapper.getMonochromeColorCode("P");
        yfColorDetail.setColorCode(colorCode);
        colorAttributeMapper.addMonochrome(yfColorDetail);

        return ResponseInfo.ok("添加成功!");
    }

    /**
     * 选择颜色
     * @param request
     * @return
     */
    @AutoPageAop
    @Override
    public ResponseInfo getColorSystemList(ColorSystemRequest request) {

        HashMap map = new HashMap();

        List<ColorSystemDto> colorSystemList = colorAttributeMapper.getColorSystemList(request);

        PageInfo list = new PageInfo(colorSystemList);

        map.put("colorSystemList", colorSystemList);
        map.put("total", list.getTotal());

        return ResponseInfo.ok(map);
    }

    /**
     * 查询颜色是否存在
     * @param color
     * @return
     */
    @Override
    public ResponseInfo getYfColorDetail(String color) {
        ColorSystemDto yfColorDetail = colorAttributeMapper.getYfColorDetail(color);
        return ResponseInfo.ok(yfColorDetail);
    }
}
